package com.yh.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yh.bean.IntegralGoods;
import com.yh.service.IntegralGoodsService;

@Controller
public class IntegralGoodsController {
	@Autowired
	private IntegralGoodsService  IntegralGoodsServiceImpl;	
//================================================================================================================		
//查询积分商品	
	@RequestMapping("/loadIntegralGoods")
	@ResponseBody
	public List<IntegralGoods> loadIntegralGoods(){		
   List<IntegralGoods> igList=IntegralGoodsServiceImpl.loadIntegralGoods();	
		return igList;
	}	
//================================================================================================================	
//添加积分商品
	@RequestMapping("/addIntegralGoods")
	public String addIntegralGoods(IntegralGoods ig,HttpServletRequest request,@RequestParam(value="ingIm")MultipartFile mf){	
		System.out.println("测试测试查收测试测试==========");
		String realPath= request.getServletContext().getRealPath("/images");
		String uploadPath=realPath+"/"+ mf.getOriginalFilename();
		String fileName= mf.getOriginalFilename();
		System.out.println("文件名字********"+fileName);
		System.out.println("上传的文件名称为："+fileName);
		ig.setIngImage(fileName);
		String name=mf.getName();
		System.out.println("属性名为："+name);
		try {						
			 IOUtils.copy(mf.getInputStream(), new FileOutputStream(uploadPath));			
		} catch (IOException e) {
			e.printStackTrace();
		}							 							
		 IntegralGoodsServiceImpl.addIntegralGoods(ig);		
			return "../index";		
	}
//================================================================================================================
	//删除积分商品
	@RequestMapping("deleteIntegralGoods")
	public String deleteIntegralGoods(Integer ingId){		
		IntegralGoodsServiceImpl.deleteIntegralGoods(ingId);		 
		return "../index";
	}
//================================================================================================================
   //修改积分商品	
	@RequestMapping("updateIntegralGoods")
	public String updateIntegralGoods(IntegralGoods ig,HttpServletRequest request,@RequestParam(value="ing")MultipartFile mf){
		System.out.println("测试测试查收测试测试==========");
		String realPath= request.getServletContext().getRealPath("/images");
		String uploadPath=realPath+"/"+ mf.getOriginalFilename();
		String fileName= mf.getOriginalFilename();
		System.out.println("文件名字********"+fileName);
		System.out.println("上传的文件名称为："+fileName);
		ig.setIngImage(fileName);
		System.out.println("================"+ig);
		String name=mf.getName();
		System.out.println("属性名为："+name);
		try {						
			 IOUtils.copy(mf.getInputStream(), new FileOutputStream(uploadPath));			
		} catch (IOException e) {
			e.printStackTrace();
		}							 							
		IntegralGoodsServiceImpl.updateIntegralGoods(ig);	
		return "../index";			
	}
	//===============================================================================================================================

	
	
}
